<?php
    $url = new Url(); 
    $forder_upload = "banner";  
    $link_url = Yii::app()->params["urlImages"]."upload.php?forder_upload=".$forder_upload;
    $arr_status = LoadConfig::$status;
    $arr_type_cat = LoadConfig::$type_cat;
    $groups = LoadConfig::$group_auto_adv;
?>
<link rel="stylesheet" href="<?php echo Yii::app()->params['urlImages']; ?>lib_upload/css/swfupload.css" type="text/css" />
<script type="text/javascript" src="<?php echo Yii::app()->params['urlImages']; ?>lib_upload/js/swfupload.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->params['urlImages']; ?>lib_upload/js/swfupload.queue.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->params['urlImages']; ?>lib_upload/js/fileprogress.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->params['urlImages']; ?>lib_upload/js/handlers.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->params['tiny_url_cp'];?>js/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
    tinyMCE.init({
        mode : "exact",
        elements : "title_top,title_bottom",
        theme : "advanced",
        plugins : "autolink,lists,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist,jbimages",

        theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,fontselect,fontsizeselect,forecolor,backcolor",
        theme_advanced_buttons2 : "image,jbimages,link,unlink,emotions,|,table,tablecontrols,code",
        theme_advanced_buttons3 : "",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_blockformats : "p,h2,h3,h4",
        removeformat_selector : "h2,h3,h4,br,b,strong,em,i,pre,span",
        theme_advanced_statusbar_location : "bottom",
        theme_advanced_path : false,
        extended_valid_elements : "a[!href|target|title],img[src|border=0|alt|title|hspace|vspace|width|height|align|name],br,strong/b,em/i,u,li,ul,ol",
        entity_encoding : "raw",
        //content_css : "../css/content.css",
        textarea_trigger : 'content_body',
        theme_advanced_resize_horizontal : true,
        theme_advanced_resizing : true,
        width:700,
        height:150,
        'editorTemplate' : 'full', 
        'useSwitch':false, 
        convert_urls : false,
        inline_styles : false,
        formats : {
            bold : {inline : 'strong'},
            italic : {inline : 'em'},
            underline : {inline : 'u'}
        },
        setup : function(ed) {
            ed.onKeyUp.add(function(ed, e) {    
                var strip = (tinyMCE.activeEditor.getContent()).replace(/(<([^>]+)>)/ig,"");
                var text = " " +  strip.length + " Characters ";
                tinymce.DOM.setHTML(tinymce.DOM.get(tinyMCE.activeEditor.id + '_path_row'), text);
            });
        }
    });
</script>
<script type="text/javascript">
    window.onload = function() {
        var configUploadData = {
            flash_url:"<?php echo Yii::app()->params["urlImages"];?>lib_upload/js/swfupload.swf",
            create_date:0,
            button_image_url:"<?php echo Yii::app()->params["urlImages"];?>lib_upload/images/buttonUpload.png",
            upload_url: "<?php echo $link_url;?>", 
            file_types: "*.jpg;*.jpeg;*.png;*.gif", 
            file_upload_limit : 20, 
            file_queue_limit : 1, 
            debug : false
        }
        configUpload(configUploadData); 
    }; 
    function uploadResponse(serverData){  
        try {
            $("#txtFileName").val(""); 
            var response = $.parseJSON(serverData); // eval( "(" + serverData + ")" );      
            if(response.code==404){
                alert(response.message);return false;
            }  
            var filename = response.filename;    
            var path = response.path;
            var message = response.message;
            var code = response.code;
            var extension = response.extension;
            var filesize = response.filesize;
            $("#picture").val(filename);
            $("#show_pic").html('<img src="<?php echo Yii::app()->params["urlImages"].$forder_upload.'/';?>'+path +'/'+ filename +'" alt="pic">')
        } catch (e) {

        };
    }

    function ajaxSaveBanner(){
        tinyMCE.triggerSave();
        var title_top = $("#title_top").val();
        var title_bottom = $("#title_bottom").val();
        var picture = $("#picture").val();
        var link = $("#link").val();
        var group_id = $("#group_id").val();
        var status = $("#status").val();
        var target = $("#target").val();
        var strUrl = "<?=$url->createUrl("autoAdv/ajaxSaveBanner") ?>";
        $.ajax({
            type: "POST",
            url: strUrl,
            data: {
                title_top:title_top,
                title_bottom:title_bottom,
                picture:picture,
                link:link,
                group_id:group_id,
                content_id:$("#content_id").val(),
                content_type:$("#content_type").val(),
                target:target,
                status:status
            },
            success: function(msg){
                if(msg == 1){
                    alert('Thêm thành công');
                    window.location = '<?php echo $url->createUrl("autoAdv/index")?>';
                }else{
                    $("#show_error").html(msg);
                }
            },
            beforeSend:function(){
                $("#button_save").attr("disabled","disabled");
            },
            complete:function(){
                $("#button_save").removeAttr("disabled"); 
            }          
        });
    }
</script>
<div class="main clearfix">
    <div class="box clearfix bottom30">
        <ul class="form4">
            <li class="clearfix"><label><strong>Tiêu đề trên </strong>:</label>
                <div class="filltext">
                    <textarea cols="5" rows="3" style="width:400px; height:40px" id="title_top" name="title_top"></textarea>
                </div>
            </li>
            <li class="clearfix"><label><strong>Tiêu đề dưới </strong>:</label>
                <div class="filltext">
                    <textarea cols="5" rows="3" style="width:400px; height:40px" id="title_bottom" name="title_bottom"></textarea>
                </div>
            </li>
            <li class="clearfix"><label><strong>Ảnh banner </strong>:</label>
                <div class="filltext">
                    <p>
                        <input type="hidden" id="txtFileName" readonly="readonly"/>
                        <input type="text" id="urlFile" style="border:1px solid #DFDFDF; width: 200px;">                            
                        <span id="spanButtonPlaceHolder"></span>
                    </p>
                    <p><i>Định dạng file:*.jpg;*.jpeg;*.png;*.gif (Dung lượng ko được quá 2 MB)</i></p>
                    <p><i>Kích thước đề nghị 320x50</i></p>
                    <br/>
                    <div class="fieldset flash" id="fsUploadProgress">
                        <span class="legend">File upload</span>
                    </div>
                    <input type="hidden" id="picture" value=""/>
                    <p id="show_pic"></p>
                </div>
            </li>
            <li class="clearfix"><label><strong>Link </strong>:</label>
                <div class="filltext">
                    <input type="text" id="link" value="" style="width:400px;"/>
                    <br/><i>
                        (Nhập bắt đầu từ dấu / của link cần chèn). <br/>
                        VD: http://iphone.hayquadi.com/m-sexy.html -> nhập vào form: /m-sexy.html 
                    </i>
                </div>
            </li>
            <li class="clearfix"><label><strong>Nhóm</strong>:</label>
                <div class="filltext">
                    <select style="width:203px;" id="group_id">
                        <?php foreach($groups as $key=>$value){?>
                            <option value="<?php echo $key;?>"><?php echo $value["name"]?></option>
                            <?php }?>
                    </select>
                </div>
            </li>
            <li class="clearfix"><label><strong>Kiểu item</strong>:</label>
                <div class="filltext">
                    <select style="width:203px;" id="content_type">
                       
                        <?php foreach($arr_type_cat as $key=>$value){?>
                            <option value="<?php echo $key;?>"><?php echo $value?></option>
                            <?php }?>
                        <option value="0">Ứng dụng</option> 
                    </select>
                </div>
            </li>
            <li class="clearfix"><label><strong>Mã item: </strong>:</label>
                <div class="filltext">
                    <input type="text" id="content_id" value="" style="width:400px;"/>                  
                </div>
            </li>
            <li class="clearfix"><label><strong>Trạng thái</strong>:</label>
                <div class="filltext">
                    <select style="width:203px;" id="status">
                        <?php foreach($arr_status as $key=>$value){?>
                            <option value="<?php echo $key;?>"><?php echo $value?></option>
                            <?php }?>
                    </select>
                </div>
            </li> 
            <li class="clearfix"><label><strong>Kiểu mở</strong>:</label>
                <div class="filltext">
                    <select style="width:203px;" id="target">
                        <option value="0">Tab hiện tại</option> 
                        <option value="1">Tab mới</option>                   
                    </select>
                </div>
            </li>
            <li class="clearfix"><label>&nbsp;</label>
                <div class="filltext">
                    <input id="button_save" onclick="ajaxSaveBanner();" type="button" value=" Thêm mới " class="btn-bigblue"> 
                    &nbsp;
                    <input onclick="history.go(-1)" type="button" value=" Quay lại " class="btn-bigblue">
                </div>
            </li>
            <li class="clearfix"><label>&nbsp;</label>
                <div class="filltext" style="color: red;" id="show_error"></div>
            </li>
        </ul>

    </div>
</div>